package main

import (
	servicestub "example/service/stub"
	"example/service_impl"
	dynamicstub "example/servicedynamic/stub"
	"example/servicedynamic_impl"
	"flag"
	"fmt"
	"gitee.com/dennis-kk/rpc-go-backend/idlrpc"
	idlnet "gitee.com/dennis-kk/rpc-go-backend/idlrpc/net"
	"os"
	"rpc-repo/gomock/utiltool"
	"strings"
)

var (
	serviceptr *service_impl.ServiceClient
	dynamicptr *servicedynamic_impl.ServiceDynamicClient
)

func printHelp() {
	bin := os.Args[0]
	if i := strings.LastIndex(bin, "/"); i != -1 {
		bin = bin[i+1:]
	}

	fmt.Printf("Usage %s -I example.idl -O impl/goservice", bin)
	flag.PrintDefaults()
}

var (
	port     string
	addr     string
	accepter *utiltool.MockAccept
)

func main() {

	flag.Usage = printHelp //解析出错时候 调用的默认方法
	flag.StringVar(&port, "p", "", "listen port")
	flag.StringVar(&addr, "h", "", "address")

	flag.Parse()

	serviceptr = &service_impl.ServiceClient{}
	dynamicptr = &servicedynamic_impl.ServiceDynamicClient{}

	accepter = utiltool.NewMockAccept()
	if accepter == nil {
		panic("accepter")
	}

	if err := idlnet.AddListener("tcp", addr+":"+port, accepter); err != nil {
		panic(err)
	}

	err := idlrpc.RegisterService(serviceptr.GetUUID(), servicestub.NewServiceStub(), serviceptr)
	if err != nil {
		fmt.Printf("Load Service error: %v !!!\n", err)
		return
	}

	err = idlrpc.RegisterService(dynamicptr.GetUUID(), dynamicstub.NewServiceDynamicStub(), dynamicptr)
	if err != nil {
		fmt.Printf("Load ServiceDynamic error: %v !!!\n", err)
		return
	}

	idlnet.Start()
	idlrpc.Start()
	for {
		//update net work
		//idlnet.Tick()
		//update rpc
		idlrpc.Tick()
	}
}
